const mediaSoup = require("mediasoup");
const config = require("./config");
const room = require("./room")

/**
 * 房间信息存储
 */
class RoomMap {
    constructor() {
        this._rooms = new Map();
    }

    async init() {
        this._worker = await mediaSoup.createWorker(
            {
                logLevel: config.mediasoup.workerSettings.logLevel,
                logTags: config.mediasoup.workerSettings.logTags,
                rtcMinPort: Number(config.mediasoup.workerSettings.rtcMinPort),
                rtcMaxPort: Number(config.mediasoup.workerSettings.rtcMaxPort)
            }
        );
        this._rtcServer = await this._worker.createWebRtcServer(config.mediasoup.webRtcServerOptions)
    }

    async set(roomId, room) {
        this._rooms.set(roomId, room)
    }

    async initRoom(roomId) {
        const room = this.get(roomId)
        if (room !== undefined) {
            await room.init(this._worker, this._rtcServer)
        }
    }

    get(roomId) {
        return this._rooms.get(roomId)
    }

    getRooms(){
        return this._rooms
    }


}

module.exports = RoomMap
